<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('已办任务') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('标题')" prop="title">
        <el-input
          v-model="queryParams.title"
          size="small"
          :placeholder="$t('标题')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('申请人')" prop="applyUserId">
        <UserSelect
          v-model="queryParams.applyUserId"
          size="small"
          :placeholder="this.$t('选择用户')"
        />
      </el-form-item>
      <el-form-item :label="$t('流程名称')" prop="processName">
        <el-input
          v-model="queryParams.processName"
          size="small"
          :placeholder="$t('流程名称')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      tooltip-effect="darkTable"
      :data="todoList"
      border
    >
      <el-table-column :label="$t('标题')" align="left" prop="title" :show-overflow-tooltip="true" min-width="40%">
        <template slot-scope="scope">
          <el-link type="primary" @click="openTask(scope.row)">{{ scope.row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请人')" align="center" prop="applyNickName" width="100" />
      <el-table-column :label="$t('申请时间')" align="center" prop="startTime" class-name="small-padding fixed-width" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('办理环节')" align="center" prop="taskName" class-name="small-padding fixed-width" width="130">
        <template slot-scope="scope">
          <span>{{ $t(scope.row.taskName) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('流程名称')" align="center" prop="processName" class-name="small-padding fixed-width" width="130">
        <template slot-scope="scope">
          <span>{{ $t(scope.row.processName) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">

        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            @click="withdraw(scope.row)"
          >{{ $t('撤回') }}</el-button>
          <el-button
            size="small"
            type="text"
            @click="comments(scope.row)"
          >{{ $t('补充意见') }}</el-button>
          <el-button
            size="small"
            type="text"
            @click="ccProcess(scope.row)"
          >{{ $t('传阅') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :visible.sync="formOpen" :title="$t('任务详情')" :close-on-click-modal="false" custom-class="dialogForm">
      <ProcessInfoForm ref="processInfoForm" />
    </el-dialog>

    <el-dialog :visible.sync="ccProcessOpen" :title="$t('流程传阅')" :close-on-click-modal="false" width="30%">
      <el-form ref="ccForm" :model="ccForm" :inline="true">
        <user-select v-model="ccForm.ccUser" :multiple="true" clearable />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelCcProcess">{{ $t('取消') }}</el-button>
        <el-button type="primary" @click="submitCcProcess">{{ $t('确定') }}</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="commentsOpen" :title="$t('补充意见')" :close-on-click-modal="false" width="50%">
      <el-form ref="commentsForm" :model="commentsForm" :inline="true" :rules="commentsFormRules">
        <el-row>
          <el-col :span="24">
            <el-input v-model="commentsForm.supplementaryComments" type="textarea" :placeholder="$t('补充意见')" show-word-limit maxlength="1000" />
          </el-col>
          <el-col :span="24">
            <FileTable :list.sync="commentsForm.fileList" data-type="act_approval" show-type="small" />
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelCommentsProcess">{{ $t('取消') }}</el-button>
        <el-button type="primary" @click="supplementaryComments">{{ $t('确定') }}</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  findSupplementaryComments,
  hiList,
  saveSupplementaryComments,
  withdrawCheck,
  withdrawTask
} from '@/api/act/task/actTask'
  import ProcessInfoForm from '../process/processInfoForm'
  import { ccProcess } from '@/api/act/process/actProcess'
export default {
  components: {
    ProcessInfoForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 待办表格数据
      todoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      formOpen: false,
      // 传阅用户弹出层是否展示
      ccProcessOpen: false,
      // 补充意见
      commentsOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        applyUserId: '',
        processName: undefined
      },
      wfMenuProcessData: {},
      // 表单参数
      form: {},
      // 流程传阅表单
      ccForm: {
        actId: undefined,
        ccUser: []
      },
      // 补充意见表单
      commentsForm: {
        id: undefined,
        taskId: undefined,
        supplementaryComments: undefined
      },
      commentsFormRules: {
        supplementaryComments: [{ required: true, message: this.$t('必填信息'),	trigger: ['blur', 'change'] }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询待办列表 */
    getList() {
      this.loading = true
      hiList(this.queryParams).then(response => {
        this.todoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.formOpen = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 打开流程详情页面
    openTask(row) {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.processInfoForm.init(row)
      })
    },
    // 流程撤回
    withdraw(row) {
      withdrawCheck(row.id).then(response => {
        if (response.data !== '') {
          this.$alert(this.$t(response.data), this.$t('提示信息'), {
            dangerouslyUseHTMLString: true
          })
        } else {
          this.$confirm(this.$t('是否确认撤回?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
          }).then(function() {
            return withdrawTask(row.id)
          }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('撤回成功'))
          }).catch(function() {
          })
        }
      })
    },
    // 补充意见
    comments(row) {
      this.commentsOpen = true
      this.loading = true
      findSupplementaryComments(row).then(response => {
          this.commentsForm = response.data
        this.loading = false
      })
    },
    supplementaryComments() {
      this.loading = true
      saveSupplementaryComments(this.commentsForm).then(response => {
        if (response.code === 200) {
          this.msgSuccess(this.$t('保存成功'))
          this.cancelCommentsProcess()
        } else {
          this.msgError(response.msg)
        }
        this.loading = false
      })
    },
    // 关闭补充意见页面
    cancelCommentsProcess() {
      this.commentsOpen = false
      this.commentsForm.supplementaryComments = undefined
      this.getList()
    },
    // 抄送流程
    ccProcess(row) {
      this.ccProcessOpen = true
      this.ccForm.actId = row.actId
      console.log(row)
    },
    // 关闭抄送页面
    cancelCcProcess() {
      this.ccProcessOpen = false
    },
    // 发送抄送
    submitCcProcess() {
      if (this.ccForm.ccUser.length !== 0) {
        ccProcess(this.ccForm).then(() => {
          this.msgSuccess(this.$t('传阅成功'))
          this.ccProcessOpen = false
        })
      } else {
        this.msgError(this.$t('请选择传阅用户'))
      }
    }
  }
}
</script>
